package it.blog.server;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.*;
import javax.xml.ws.wsaddressing.W3CEndpointReference;


@WebService
public class BlogPortal {

	private IDataAccess dataAccess;
	private Map<String, AuthBlogPortal> accounts=new HashMap<String, AuthBlogPortal>();


	public synchronized
	W3CEndpointReference createUserSession(String username, String password, String sessionId) {	
		AuthBlogPortal session= new AuthBlogPortal(username, password);
		User user=session.getSessionUser();
		if(user==null)
			return null;
		else{
			   accounts.put(sessionId, session);
			return AuthBlogPortal.manager.export(session);
		}
	}


	public synchronized boolean addUser(User user,String sessionId) {
		boolean res=false;
		AuthBlogPortal session= new AuthBlogPortal(user);
		if(accounts.get(sessionId)==null){
			res=session.createUser(user);
			if(res)
	        accounts.put(sessionId, session);

		}
		return res;
	}

	public synchronized User getUserSession(String sessionId){		 		 
		AuthBlogPortal session=accounts.get(sessionId);
		if(session== null)
			return null;
		return session.getSessionUser();
	}
	
	
	public User getPublicInfoUser(Integer postedUserID){
		User user = null;
		getDataAccess();
		try {
			//Prendo tutto l'ogetto User
			user = dataAccess.getUser(postedUserID);
			//Setto a null tutti i campi che nn sono pubblici
			user.dateOfBirth=null;
			user.password=null;
			user.role=null;
			user.state=null;
		} catch (SQLException e) {
			return null;
		}
		return user;
	}
	

	public synchronized void removeUserSession(String sessionId){
		accounts.remove(sessionId);
	}



	public List<Blog> getBlogsOfCategory(Integer categoryId){
		
		String order="blogTitle";
		Boolean descending=false;
		
		getDataAccess();
		List<Blog> list = null;
		try {
			list = dataAccess.getBlogsOfCategory(categoryId,order,descending);
		} catch (SQLException e) {
			return null;
		}
		return list;	
	}

	public List<Post> getPostsOfCategory(Integer categoryId,Integer blogId){
		
		String order="addDate";
		Boolean descending=true;
		
		getDataAccess();
		List<Post> list = null;
		try {
			list = dataAccess.getPostsOfCategory(categoryId,blogId,order,descending);
		} catch (SQLException e) {
			return null;
		}
		return list;	

	}
	
	public Blog getBlog(Integer idBlog){

		getDataAccess();
		Blog blog=null;
		try {
			blog = dataAccess.getBlog(idBlog);
		} catch (SQLException e) {
			return null;
		}
		return blog;
	}
	
	public Blog getBlogOfUser(Integer idUser){

		getDataAccess();
		Blog blog=null;
		try {
			blog = dataAccess.getBlogOfUser(idUser);
		} catch (SQLException e) {
			return null;
		}
		return blog;
	}


	public Post getPost(Integer idPost){

		getDataAccess();	
		Post post=null;	
		try {
			post = dataAccess.getPost(idPost);
		} catch (SQLException e) {
			return null;
		}
		return post;		
	}

	public List<Blog> getBlogList(){

		String order="blogTitle";
		Boolean descending=false;

		getDataAccess();
		List<Blog> list = null;
		try {
			list = dataAccess.getBlogList(order, descending);
		} catch (SQLException e) {
			return null;
		}
		return list;	
	}


	public List<Post> getPostsOfBlog(Integer blogID){

		String order = "addDate";
		Boolean descending = true;

		getDataAccess();	
		List<Post> list = null;
		try {
			list = dataAccess.getPostsOfBlog(blogID, order, descending);
		} catch (SQLException e) {
			return null;
		}	
		return list;
	}

	public List<Comment> getCommentsOfPost(Integer blogID, Integer postID){

		String order = "addDate";
		Boolean descending=false;

		getDataAccess();	
		List<Comment> list = null;
		try {
			list = dataAccess.getCommentsOfPost(blogID, postID, order, descending);
		} catch (SQLException e) {
			return null;
		}	
		return list;


	}

	public List<BlogCategory> getBlogCategoryList(){

		getDataAccess();		
		List<BlogCategory> list = null;		
		try {
			list = dataAccess.getBlogCategoryList();
		} catch (SQLException e) {
			return null;
		}
		return list;

	}

	public List<PostCategory> getAllPostCategoryList(){

		getDataAccess();		
		List<PostCategory> list = null;
		try {
			list = dataAccess.getAllPostCategoryList();
		} catch (SQLException e) {
			return null;
		}
		return list;


	}
	
	public List<PostCategory> getPostCategoryListOfBlog(Integer blogID){

		getDataAccess();	
		List<PostCategory> list = null;
		try {
			list = dataAccess.getPostCategoryListOfBlog(blogID);
		} catch (SQLException e) {
			return null;
		}		
		return list;

	}

	private void  getDataAccess(){
		dataAccess= DataAccess.getInstance();
	}
}
